import modelExtend from 'dva-model-extend';
import { queryToconfirmOrderList } from './services/contractOrders';
import { contractOrderDetail, acceptOmsQuote,denyOmsQuote,getTenantOverall } from './services/order';
import { lwjPageModel } from 'utils/model';
import { Modal, message } from 'antd';

export default modelExtend(lwjPageModel, {
  namespace: 'confirmOmsQuoteContract',
  state: {
    items: [],
    currentContractDetail: {},
    pageOptions: {
      sort: 'updateTime',
      order: 'DESC'
    },
    isExpand: false,
    isShowContractDetailModal: false,
    isShowContractConfirmModal: false,
    tenantOverall:"",
    currentItem: {}
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/order/confirmOmsQuote') {
          dispatch({ type: 'query', payload: {} })
          dispatch({type:'tenantOverall'})
        }
      })
    },
  },
  effects: {
    * query({ payload }, { call, put, select }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({ confirmOmsQuoteContract }) => confirmOmsQuoteContract.pageOptions)
      const queryParam = yield select(({ confirmOmsQuoteContract }) => confirmOmsQuoteContract.queryParam)
      const queryPayload = {
        ...pageOptions,
        queryParam: queryParam
      }
      // queryParam.status = "PENDING_CONFIRM_OMS_QUOTED";
      // queryParam.isPending4Oms = true;
      const data = yield call(queryToconfirmOrderList, queryPayload)
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },
    * onShowDetail({
      payload
    }, { select, call, put }) {
      const data = yield call(contractOrderDetail, payload.salesOrderId, payload.contractOrderId);
      yield put({
        type: 'detailSuccess',
        payload: {
          currentContractDetail: data.result
        }
      })
      yield put({ type: 'showContractDetailModal', payload: {} })
    },
    /**
     * 通过OMS报价
     */
    * acceptOmsQuote({
      payload
    }, { select, call, put }) {
      yield call(acceptOmsQuote, payload.salesOrderId, payload.contractOrderId, payload.version);
      message.success('丽维家报价已通过');
      yield put({ 
        type: 'toggleConfirmOmsQuoteModal', 
        currentItem: {},
        isShowContractConfirmModal: false
      })
      const pageOptions = yield select(({ confirmOmsQuoteContract }) => confirmOmsQuoteContract.pageOptions)
      yield put({ type: 'query', payload: {
        ...pageOptions,
        isTablePagination: true
      }})
    },
    /**
     * 不通过OMS报价
     */
    * denyOmsQuote({
      payload
    }, { select, call, put }) {
      yield call(denyOmsQuote, payload.salesOrderId, payload.contractOrderId, payload.version);
      message.success('丽维家报价已拒绝');
      yield put({ 
        type: 'toggleConfirmOmsQuoteModal', 
        currentItem: {},
        isShowContractConfirmModal: false
      })
      const pageOptions = yield select(({ confirmOmsQuoteContract }) => confirmOmsQuoteContract.pageOptions)
      yield put({ type: 'query', payload: {
        ...pageOptions,
        isTablePagination: true
      }})
    },
    * tenantOverall({
      payload
    }, { select, call, put }) {
      const data = yield call(getTenantOverall);
      yield put({ type: 'setTenantOverall', payload: data.result })

    }
  },

  reducers: {
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    detailSuccess(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    showContractDetailModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowContractDetailModal: true
      }
    },
    toggleConfirmOmsQuoteModal(state, { currentItem,isShowContractConfirmModal }) {
      return {
        ...state,
        currentItem,
        isShowContractConfirmModal
      }
    },
    hideContractDetailModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowContractDetailModal: false
      }
    },
    setTenantOverall(state, { payload }) {
      return {
        ...state,
        ...payload,
        tenantOverall: payload
      }
    }
  }
})
